package com.uhu.code;

import com.uhu.code.help.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 206.反转链表
 * https://leetcode.cn/problems/reverse-linked-list/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution206 {
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        List<Integer> lk = new ArrayList<>();
        while(head != null) {
            lk.add(head.getVal());
            head = head.getNext();
        }
        ListNode result = new ListNode(lk.get(lk.size() - 1));
        ListNode temp = result;
        for(int i = lk.size() - 2; i >= 0; i--) {
            temp.addNode(lk.get(i));
            temp = temp.getNext();
        }
        return result;
    }
}
